package cn.liutao.order.api;

import cn.liutao.common.entity.LoginUser;
import cn.liutao.common.entity.Validation;
import cn.liutao.common.utils.ServletUtils;
import cn.liutao.common.utils.ValidUtil;
import cn.liutao.order.entity.dto.OrderCreateResultDto;
import cn.liutao.order.entity.param.OrderCreateParam;
import cn.liutao.order.service.OrderService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author liutao
 * @Description 订单创建api
 * @date 2023/11/2 9:28
 * @Version 1.0
 */
@WebServlet("/order/create")
public class OrderCreateServlet extends HttpServlet {

    private OrderService orderService;

    @Override
    public void init() throws ServletException {
        orderService = OrderService.getInstance();
    }

    /**
     * 创建订单
     * @param req
     * @param resp
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        OrderCreateParam addParam = ServletUtils.getBodyParam(req, OrderCreateParam.class);
        ValidUtil.validated(addParam, Validation.AddGroup.class);
        Integer userId = LoginUser.getUserId();
        addParam.setUserId(userId);
        OrderCreateResultDto resultDto = orderService.createOrder(addParam);
        ServletUtils.ok(resp,resultDto,"创建成功");
    }

}
